<template>
  <div class="update-gender">
    <van-picker
      show-toolbar
      title="选择性别"
      :columns="columns"
      :default-index="defaultIndex"
      @cancel="onCancel"
      @confirm="onConfirm"
    />
  </div>
</template>

<script>
import { updateUserInfo } from "@/api/user";
export default {
  name: "UpdateGender",
  components: {},
  props: {
    value: {
      type: [String, Number],
      default: 0,
    },
  },
  data() {
    return {
      columns: ["男", "女"],
      defaultIndex: this.value,
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async onConfirm(value, index) {
      console.log(value, index);
      try {
        await updateUserInfo({
          gender: index,
        });
        this.$emit("input", index);
        this.$emit("close");
        this.$toast.success("更新成功");
      } catch (error) {
        console.log(error);
        this.$toast.fail("更新失败");
      }
    },
    onCancel() {
      this.$emit("close");
    },
  },
};
</script>

<style scoped lang="less"></style>
